package org.batavi.customers;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class createNewCustomer {
	
	private WebDriver driver;
	private String baseBeUrl = "http://test.batavi.org/admin/index.php";
	private StringBuffer verificationErrors = new StringBuffer();
	private String customersPage = "http://test.batavi.org/admin/index.php?customers&customers"; 
	
	
@Before 
public void setUp() throws Exception {
	driver = new FirefoxDriver();
	driver.get(baseBeUrl);
	driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	driver.findElement(By.id("admin_login_form_user_name")).sendKeys("Admin");
	driver.findElement(By.id("admin_login_form_user_name")).sendKeys("12345");
	//locator for button Loggin should be added
	driver.findElement(By.id("")).click();
	driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public createNewCustomer () {	  
	   driver.get(customersPage);
	   //locator for button "+ New Customer" should be added. 
	   driver.findElement(By.id("")).click();
	   new Select(driver.findElement(By.id("customers_parent_id"))).selectByVisibleText("Consumer");
	   driver.findElement(By.id("general_email")).sendKeys("Test.customer1@batavi.org");
	   //ID should be changed in HTML code to general_gender_m for Gender:* selecter
	   driver.findElement(By.id("general_gender_m")).click();
	   driver.findElement(By.id("general_firstname")).sendKeys("CustomerName");
	   driver.findElement(By.id("general_lastname")).sendKeys("Last Name");
	   new Select(driver.findElement(By.id("general_days"))).selectByVisibleText("1");
	   new Select(driver.findElement(By.id("general_months"))).selectByVisibleText("February");
	   new Select(driver.findElement(By.id("general_years"))).selectByVisibleText("1997");
	   driver.findElement(By.id("general_street")).sendKeys("CustomerStreet");
	   driver.findElement(By.id("general_house_number")).sendKeys("11/12");
	   driver.findElement(By.id("general_postcode")).sendKeys("1234 AD");
	   driver.findElement(By.id("general_city")).sendKeys("CustomerCity");
	   driver.findElement(By.id("general_telephone")).sendKeys("+123 456 78 90");
	   //Id should be added to Save button 
	   driver.findElement(By.id("")).click();
	   assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("Success: The action was successfully performed."));
	   assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("Last Name"));
	   assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("CustomerName"));
}

@After
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
}

